<template>
<div>
    <!-- 头部 -->
   <van-nav-bar
  title="黑马头条-登录"
/>
<!-- 表单 -->
<van-form @submit="onSubmit">
  <van-field
    v-model="formLogin.mobile"
    name="用户名"
    label="手机号码"
    required
    placeholder="请输入手机号"
    :rules="[{ required: true, message: '请填写用户名',trigger:'onChange', pattern: /^1[3456789]\d{9}$/}]"
  />
  <van-field
    v-model="formLogin.code"
    type="password"
    name="密码"
    label="登录密码"
    required
    placeholder="请填写密码"
    :rules="[{ required: true, message: '请填写密码',trigger:'onChange' ,pattern: /^\d{6}$/}]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit" :loading="isloading" :disabled="isloading" loading-text="加载中..." >提交</van-button>
  </div>
</van-form>
    </div>
</template>
<script>
import { loginAPI } from '@/api'
import { mapMutations } from 'vuex'
import { MyNotify } from '@/utils/Notify'
export default {
  name: 'Login',
  data () {
    return {
      formLogin: {
        mobile: '13888888888',
        code: '246810'
      },
      isloading: false
    }
  },
  methods: {
    ...mapMutations(['setToken', 'setreFreshToken']),
    async onSubmit () {
      try {
        // 设置两个token
        this.isloading = true
        const res = await loginAPI(this.formLogin)
        this.setToken(res.data.data.token)
        this.setreFreshToken(res.data.data.refresh_token)
        this.isloading = false
        // 登录成功之后,跳转到layout里面
        console.log(this.$route.query.code)
        MyNotify({ type: 'success', message: '登录成功' })
        if (this.$route.query.code === '401') {
          this.$router.back()
        } else {
          this.$router.push({
            path: '/layout'
          })
        }
      } catch (err) {
        this.isloading = false
        MyNotify({ type: 'warning', message: '登陆失败' })
      }
    }
  }
}
</script>

<style lang="less" scoped>
</style>
